﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace EncuentraWEB
{
    public partial class frmLineas : System.Web.UI.Page
    {
        private ArqJsCls arq;
        private clsLineas obj;

        protected void Page_Load(object sender, EventArgs e)
        {
            var sUser = (string)Session["User"];
            if (sUser != "admin")
            {
                Response.Redirect("~\\default.aspx");
                return;
            }
            arq = new ArqJsCls();
            obj = new clsLineas();
            if (!this.IsPostBack)
            {
                try
                {
                    var objEmpresa = new clsEmpresas();
                    dropEmpresa.DataTextField = "varDenominacion";
                    dropEmpresa.DataValueField = "intIdEmpresa";
                    var dtEmpresas = objEmpresa.buscar("", "");
                    var drEmpresa = dtEmpresas.NewRow();
                    drEmpresa["intIdEmpresa"] = 0;
                    drEmpresa["varDenominacion"] ="TODOS";
                    dtEmpresas.Rows.Add(drEmpresa);
                    dropEmpresa.DataSource = dtEmpresas;
                    dropEmpresa.DataBind();
                    dropEmpresa.SelectedIndex = dropEmpresa.Items.Count - 1;
                    Buscar();
                    hidIndice.Value = "-1";
                }
                catch (Exception ex)
                {
                }
            }

        }

        protected void txtLinea_TextChanged(object sender, EventArgs e)
        {

        }

        protected void Button4_Click(object sender, EventArgs e)
        {
            Buscar();
        }

       

        protected void Button3_Click(object sender, EventArgs e)
        {
            Modificar();
        }
        protected void rdbGVRow_CheckedChanged(object sender, System.EventArgs e)
        {
            //Clear the existing selected row 
            foreach (GridViewRow oldrow in grdEmpresas.Rows)
            {
                ((RadioButton)oldrow.FindControl("rdbGVRow")).Checked = false;
            }

            //Set the new selected row
            RadioButton rb = (RadioButton)sender;
            GridViewRow row = (GridViewRow)rb.NamingContainer;
            ((RadioButton)row.FindControl("rdbGVRow")).Checked = true;
        }

        protected void Button6_Click(object sender, EventArgs e)
        {
            Eliminar();
        }

        protected void grdEmpresas_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grdEmpresas.PageIndex = e.NewPageIndex;
            Buscar();
        }

        #region Metodos Privados
        private void Buscar()
        {

            DataTable dt = obj.buscar(0, txtLinea.Text);
            int b = dt.Rows.Count;
            int a = b %= 10;
            if (a != 0)
            {
                for (int i = 1; i <= (10 - a); i++)
                {
                    dt.Rows.Add();
                }
            }

            try
            {
                this.grdEmpresas.DataSource = dt;
                this.grdEmpresas.DataBind();
            }
            catch (Exception ex)
            {
            }
        }
        private void Agregar()
        {
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            arq.Funciones(this, "ShowEditModal('frmamLinea.aspx?id=I&id1=0')");
           
            ModalPop.Show();
            

        }
        private void Modificar()
        {
            if (Convert.ToInt32(hidIndice.Value) >= 0)
            {
                var id = grdEmpresas.Rows[Convert.ToInt32(hidIndice.Value)].Cells[1].Text;
                Response.Cache.SetCacheability(HttpCacheability.NoCache);
                arq.Funciones(this, "ShowEditModal('frmamLinea.aspx?id=M&id1=" + id + "')");
               
                ModalPop.Show();
                
            }
            else
            {
                arq.Msg(this, "No se ha seleccionado ninguna fila");
            }
        }
        private void Eliminar()
        {
            if (Convert.ToInt32(hidIndice.Value) >= 0)
            {
                var id = Convert.ToInt32(grdEmpresas.Rows[Convert.ToInt32(hidIndice.Value)].Cells[1].Text);
                obj.Eliminar(id);
                Buscar();
            }
            else
            {
                arq.Msg(this, "No se ha seleccionado ninguna fila");
            }
        }
        #endregion

        protected void ImageButton4_Click(object sender, ImageClickEventArgs e)
        {
            Buscar();
        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            Agregar();
        }

        protected void btnImgEditar_Click(object sender, ImageClickEventArgs e)
        {
            Modificar();
        }

        protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
        {
            Modificar();
        }

        protected void ImageButton3_Click(object sender, ImageClickEventArgs e)
        {
            Eliminar();
        }

    



    }
}
